class CalendarHelp:
    def __init__(self):
        from time import localtime
        self.Months = {'1':'Jan','2':'Feb','3':'Mar','4':'Apr','5':'May','6':'Jun','7':'Jul','8':'Aug','9':'Sep','10':'Oct','11':'Nov','12':'Dec'}
        #Python handles time in a funny fashion
        self.localtime = localtime()
        self.current_year = self.localtime[0]
        self.current_month = self.localtime[1]
        self.current_day = self.localtime[2]
        self.current_weekday = self.localtime[6]
        #self.days_in_current_month = monthrange(self.current_year, self.current_month)[1] #monthrange (imported from the calendar module) returns two values: the first day of the month AND the number of days in the month.


    def getCalendarDate(self, grid_number): #0-6 is current week. 7-13 is for next week. 
        from calendar import monthrange
        #Each day in my two-week Calendar view is given an id. Numbers 0-6 represent the days in the first week on the Calendar.
        #This function recieves the id number and returns the Y-M-D corresponding to that id.
        #The function compares the id with the current_day, which was defined at the instantiation of the class
        #Returns a tuple (YYYY, MM, DD)
        #######NON-RETURN VARIABLES###########
        current_weekday = self.current_weekday #An integer corresponding to the current day of the week.
        current_day = self.current_day #An integer corresponding to the current date(Day) of the month.

        ############ The Date Function is relative to each grid on the Calendar
        ## Compare the grid value with the current date of the week
        ## Basically, the higher the grid/column value, the later the date will be.
        ## Alright, I know how to explain it: Localtime returns the present date and the present weekday. We compare the weekday of the grid objects with the current weekday, and then adjust the date of the grid object accordingly.

        day = current_day + grid_number - current_weekday

        ## You might assume that grid_number will always be in range(1,14). However, grid_number is arbitrary, so that could represent calendar grids far into the future or far into the past.
        ## The statements above just adjust the grid_number relative to the current day and current weekday. It will not tell what the date is.
        ## To find out what the date of grid_number should be, we have to find out which month grid_number will fall in.

        ## Lets get some initial values. I don't want to use self.values, because self.values should always be values of the present time.
        ## These values will be modified in loops.
        month = self.current_month
        year = self.current_year
        days_in_month = (monthrange(year, month)[1])

        ## Now figure out both the month and date of the box represented by grid_number
        ## There are 2 branches: 1 for the future, one for the past.
        if day > 1: #Finding the month of grid_numbers representing the present or future days
            while day > days_in_month: #Check if the grid_number is in the current month or in a future month
                # If grid_number is in a future month . . .
                day = day - days_in_month # Subtract the number of days in the month we are checking against
                month = month+1 # Then, set month to the next month
                if month == 13: # If the next month is January of next year, set year to next year, and set month to 1.
                    month =1
                    year = year + 1
                days_in_month = (monthrange(year, month)[1])
        elif day < 1: #Finding the month of grid_numbers representing days
            while day < 0:
                day = day + days_in_month
                month = month - 1
                if month == 0: #If the previous month is December of previous year, set year to previous year, and set month to 12.
                    month = 12
                    year = year - 1
                days_in_month = (monthrange(year, month)[1])
        ISO_format = str(year)+"-"+str(month)+"-"+str(day)
        return (year, month, day)

    def getCalendarDateReadable(self, date_tuple=None):
        if type(date_tuple) != tuple: #Usually a number
            date_tuple = self.getCalendarDate(date_tuple) #(YYYY, MM, DD)
        year=str(date_tuple[0])
        month = str(date_tuple[1])
        day = str(date_tuple[2])
        month_string = self.Months[month]
        return (month_string+" "+day)#Months is the dictionary I defined above. month is an integer. I cast to string, then ask Months for the name of the month




    
